package org.jsbd.boss.util;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class ParseXmlUtil {

    public static Map<String, String> parseXml(StringBuilder strXML) throws Exception {
        DocumentBuilder db = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        // 把文件解析成DOCUMENT类型
        Document document = db.parse(new InputSource(new StringReader(strXML.toString())));
        NodeList lists = document.getChildNodes();
        return showElem(lists);
    }

    public static Map<String, String> parseXmlByStr(String strXML) throws Exception {
        DocumentBuilder db = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        // 把文件解析成DOCUMENT类型
        Document document = db.parse(new InputSource(new StringReader(strXML)));
        NodeList lists = document.getChildNodes();
        return showElem(lists);
    }

    public static Map<String, String> showElem(NodeList list) {
        Map<String, String> mMap = new HashMap<String, String>();
        for (int i = 0; i < list.getLength(); i++) {
            // 得到父节点
            Node n = list.item(i);
            // 子节点
            NodeList childList = n.getChildNodes();
            for (int x = 0; x < childList.getLength(); x++) {
                Node childNode = childList.item(x);
                // 判断取出的值是否属于Element元素,目的是过滤掉
                if (childNode instanceof Element) {
                    // 得到子节点的名字
                    String childNodeName = childNode.getNodeName();
                    // 得到子节点的值
                    if (childNode.getFirstChild() != null) {

                        String childNodeValue = childNode.getFirstChild()
                                .getNodeValue();
                        if (childNodeName != null && !"".equals(childNodeName)
                                && childNodeValue != null
                                && !"".equals(childNodeValue)) {
                            mMap.put(childNodeName, childNodeValue);
                        }
                    }
                }
            }
        }
        return mMap;
    }

    public static Map<String, String> parseXmlFromZc(StringBuilder strXML)
            throws Exception {
        DocumentBuilder db = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        // 把文件解析成DOCUMENT类型
        Document document = db.parse(new InputSource(new StringReader(strXML
                .toString())));
        NodeList lists = document.getChildNodes();
        return showZcElem(lists);
    }

    public static Map<String, String> showZcElem(NodeList list) {
        Map<String, String> mMap = new HashMap<String, String>();
        for (int i = 0; i < list.getLength(); i++) {
            Node n = list.item(i);// 得到父节点
            // 子节点
            NodeList childList = n.getChildNodes();
            for (int x = 0; x < childList.getLength(); x++) {
                Node childNode = childList.item(x);
                // 判断取出的值是否属于Element元素,目的是过滤掉
                if (childNode instanceof Element) {
                    // 得到子节点的名字
                    String childNodeName = childNode.getNodeName();
                    // 得到子节点的值

                    String childNodeValue = childNode.getFirstChild()
                            .getNodeValue();
                    if (childNodeName != null && !"".equals(childNodeName)
                            && childNodeValue != null
                            && !"".equals(childNodeValue)) {
                        mMap.put(childNodeName, childNodeValue);
                    }

                    for (int j = 0; j < childNode.getChildNodes().getLength(); j++) {
                        Node child2Node = childNode.getChildNodes().item(j);
                        if (child2Node == null
                                || child2Node.getFirstChild() == null) {
                            continue;
                        }

                        for (int k = 0; k < child2Node.getChildNodes()
                                .getLength(); k++) {
                            Node child3Node = child2Node.getChildNodes()
                                    .item(k);
                            String child3NodeName = child3Node.getNodeName();
                            String child3NodeValue = child3Node.getFirstChild()
                                    .getNodeValue();
                            if (child3NodeName != null
                                    && !"".equals(child3NodeName)
                                    && child3NodeValue != null
                                    && !"".equals(child3NodeValue)) {
                                mMap.put(child3NodeName, child3NodeValue);
                            }
                        }

                    }

                }
            }
        }
        return mMap;
    }
}
